package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import top.maof.book.dao.RolePermMapper;
import top.maof.book.model.RolePerm;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.service.RolePermService;
import top.maof.book.utils.Utils;


import java.util.List;

/**
 * Service实现
 *
 * @author 久念
 * @date 2022/03/03 19:37
 */
@Service
public class RolePermServiceImpl extends ServiceImpl<RolePermMapper, RolePerm> implements RolePermService {

    @Autowired
    private RolePermMapper rolePermMapper;

    @Override
    public Integer create(RolePerm rolePerm) {
        rolePermMapper.insert(rolePerm);
        return rolePerm.getId();
    }

    @Override
    public void deleteById(Integer id) {
        rolePermMapper.deleteById(id);
    }

    @Override
    public void delete(RolePerm rolePerm) {
        UpdateWrapper<RolePerm> deleteWrapper = new UpdateWrapper<>(rolePerm);
        rolePermMapper.delete(deleteWrapper);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        rolePermMapper.deleteBatchIds(ids);
    }


    @Override
    public RolePerm findById(Integer id) {
        return rolePermMapper.selectById(id);
    }

    @Override
    public RolePerm findOne(RolePerm rolePerm) {
        QueryWrapper<RolePerm> wrapper = new QueryWrapper<>(rolePerm);
        return rolePermMapper.selectOne(wrapper);
    }

    @Override
    public List<RolePerm> findList(RolePerm rolePerm) {
        QueryWrapper<RolePerm> wrapper = new QueryWrapper<>(rolePerm);
        return rolePermMapper.selectList(wrapper);
    }

    @Override
    public PageData<RolePerm> findPage(PageWrap<RolePerm> pageWrap) {
        IPage<RolePerm> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<RolePerm> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(RolePerm::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getRoleId() != null) {
            queryWrapper.lambda().eq(RolePerm::getRoleId, pageWrap.getModel().getRoleId());
        }
        if (pageWrap.getModel().getPermId() != null) {
            queryWrapper.lambda().eq(RolePerm::getPermId, pageWrap.getModel().getPermId());
        }
        for (PageWrap.SortData sortData : pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(rolePermMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(RolePerm rolePerm) {
        QueryWrapper<RolePerm> wrapper = new QueryWrapper<>(rolePerm);
        return rolePermMapper.selectCount(wrapper);
    }
}
